import { $http } from '@escook/request-miniprogram'
// 导入vuex
import store from '../store/index.js'
uni.$http = $http
$http.baseUrl = 'http://god-express-gateway-t.itheima.net/driver'
// 请求拦截器
$http.beforeRequest = function (options) {
    // 从store中获取token 
    // console.log(options,'options')
    const whiteList = ["http://god-express-gateway-t.itheima.net/driver/login/account"]
    const token = store.state.token
    if (token) {
        options.header = {
            Authorization: uni.getStorageSync('token')
        }
    }else{
        if(!whiteList.includes(options.url)){
            // console.log('发送的请求,不在白名单')
            uni.showToast({
                title:'请先登录',
                icon:'none'
            })
            uni.navigateTo({
                url:'/subpkg/login/login'
           })
        }else{
            // console.log('发送的请求,在白名单')
        }
    }
}
// 响应拦截器
$http.afterRequest = function (res) {
    // console.log(res,'res')
    //如果响应码是401,则跳转到登录页面
    // console.log(res,'res')
    if(res.statusCode === 401){
        // uni.showToast({
        //     title:'请先登录',
        //     icon:'none'
        // })
        uni.navigateTo({
            url:'/subpkg/login/login'
       })
    }
}